package org.ysh.vo;

import lombok.Data;
import org.ysh.domain.Fonds;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 全宗的树结构VO
 */
@Data
public class FondsTreeNodeVo {

    private String name;

    private String fondsCode;

    private Boolean spread = false;

    private String icon = "&#xe6af;";

    private List<FondsTreeNodeVo> children;

    public static List<FondsTreeNodeVo> buildTree(List<Fonds> fondsList){
        List<FondsTreeNodeVo> fondsTreeNodeVos = new ArrayList<>();
        if(fondsList==null || fondsList.isEmpty()){
            return fondsTreeNodeVos;
        }

        return findChildren("-1",fondsList);
    }

    private static List<FondsTreeNodeVo> findChildren(String fondsCode , List<Fonds> fondsList) {
        List<FondsTreeNodeVo> children = new ArrayList<>();
        for(Fonds fonds : fondsList){
            if(fonds.getParentCode().equals(fondsCode)){
                FondsTreeNodeVo fondsTreeNodeVo = new FondsTreeNodeVo();
                fondsTreeNodeVo.name = fonds.getFondsName();
                fondsTreeNodeVo.fondsCode = fonds.getFondsCode();
                children.add(fondsTreeNodeVo);
                //递归查找子节点
                fondsTreeNodeVo.setChildren(findChildren(fonds.getFondsCode(), fondsList));
            }
        }
        return children.isEmpty() ? null : children;
    }
}
